import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

// 路由懒加载
const Login = () => import('@/views/Login.vue')
const Register = () => import('@/views/Register.vue')
const Home = () => import('@/views/Home.vue')
const MerchantList = () => import('@/views/MerchantList.vue')
// 尚未实现的组件，先注释起来
const MerchantDetail = () => import('@/views/MerchantDetail.vue')
const Cart = () => import('@/views/Cart.vue')
const Checkout = () => import('@/views/Checkout.vue')
const Order = () => import('@/views/Order.vue')
const OrderDetail = () => import('@/views/OrderDetail.vue')
const UserCenter = () => import('@/views/UserCenter.vue')
const UserInfo = () => import('@/views/user/UserInfo.vue')
const UserAddress = () => import('@/views/user/UserAddress.vue')
const UserOrder = () => import('@/views/user/UserOrder.vue')

// 商家模块组件
const MerchantLayout = () => import('@/views/merchant/Layout.vue')
const MerchantDashboard = () => import('@/views/merchant/Dashboard.vue')
const FoodManagement = () => import('@/views/merchant/FoodManagement.vue')
const OrderManagement = () => import('@/views/merchant/OrderManagement.vue')
const Profile = () => import('@/views/merchant/Profile.vue')
const ReviewManagement = () => import('@/views/merchant/ReviewManagement.vue')
const Statistics = () => import('@/views/merchant/Statistics.vue')
const PromotionManagement = () => import('@/views/merchant/PromotionManagement.vue')
const MerchantApply = () => import('@/views/merchant/Apply.vue')

// 管理员组件
const AdminLogin = () => import('@/views/admin/Login.vue')
const AdminLayout = () => import('@/views/admin/Layout.vue')
const AdminDashboard = () => import('@/views/admin/Dashboard.vue')
const UserManagement = () => import('@/views/admin/UserManagement.vue')
const MerchantManagement = () => import('@/views/admin/MerchantManagement.vue')
const AdminOrderManagement = () => import('@/views/admin/OrderManagement.vue')
const AdminStatistics = () => import('@/views/admin/Statistics.vue')
const Settings = () => import('@/views/admin/Settings.vue')
const BannerManagement = () => import('@/views/admin/BannerManagement.vue')
const AdminProfile = () => import('@/views/admin/Profile.vue')

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: false }
    
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/merchants',
    name: 'MerchantList',
    component: MerchantList,
    meta: { requiresAuth: false }
  },
  // 尚未实现的路由，先注释起来
  
  {
    path: '/merchants/:id',
    name: 'MerchantDetail',
    component: MerchantDetail,
    props: true,
    
  },
  
  {
    path: '/cart',
    name: 'Cart',
    component: Cart,
    meta: { requiresAuth: true }
  },
  
  {
    path: '/checkout',
    name: 'Checkout',
    component: Checkout,
    meta: { requiresAuth: true }
  },
  
  {
    path: '/orders',
    name: 'Order',
    component: Order,
    meta: { requiresAuth: true }
  },
  {
    path: '/orders/:id',
    name: 'OrderDetail',
    component: OrderDetail,
    props: true,
    meta: { requiresAuth: true }
  },
  {
    path: '/user',
    name: 'UserCenter',
    component: UserCenter,
    meta: { requiresAuth: true },
    children: [
      {
        path: 'info',
        name: 'UserInfo',
        component: UserInfo
      },
      {
        path: 'address',
        name: 'UserAddress',
        component: UserAddress
      },
      {
        path: 'orders',
        name: 'UserOrder',
        component: UserOrder
      }
    ]
  },
  
  // 商家模块路由
  {
    path: '/merchant',
    component: MerchantLayout,
    meta: { requiresAuth: true, requiresRole: 2 },
    children: [
      {
        path: '',
        redirect: '/merchant/dashboard'
      },
      {
        path: 'dashboard',
        name: 'MerchantDashboard',
        component: MerchantDashboard,
        meta: { title: '商家首页' }
      },
      {
        path: 'foods',
        name: 'FoodManagement',
        component: FoodManagement,
        meta: { title: '菜品管理' }
      },
      {
        path: 'orders',
        name: 'MerchantOrderManagement',
        component: OrderManagement,
        meta: { title: '订单管理' }
      },
      {
        path: 'profile',
        name: 'MerchantProfile',
        component: Profile,
        meta: { title: '店铺信息' }
      },
      {
        path: 'reviews',
        name: 'ReviewManagement',
        component: ReviewManagement,
        meta: { title: '评价管理' }
      },
      {
        path: 'statistics',
        name: 'MerchantStatistics',
        component: Statistics,
        meta: { title: '统计分析' }
      },
      {
        path: 'promotions',
        name: 'PromotionManagement',
        component: PromotionManagement,
        meta: { title: '营销活动' }
      }
    ]
  },
  
  // 商家申请路由（普通用户可以访问）
  {
    path: '/merchant/apply',
    name: 'MerchantApply',
    component: MerchantApply,
    meta: { requiresAuth: true }
  },
  
  // 管理员路由
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: AdminLogin
  },
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAuth: true, requiresRole: 3 },
    children: [
      {
        path: '',
        redirect: '/admin/dashboard'
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: AdminDashboard,
        meta: { title: '首页' }
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: UserManagement,
        meta: { title: '用户管理' }
      },
      {
        path: 'merchants',
        name: 'MerchantManagement',
        component: MerchantManagement,
        meta: { title: '商家管理' }
      },
      {
        path: 'orders',
        name: 'AdminOrderManagement',
        component: AdminOrderManagement,
        meta: { title: '订单管理' }
      },
      {
        path: 'statistics',
        name: 'AdminStatistics',
        component: AdminStatistics,
        meta: { title: '统计分析' }
      },
      {
        path: 'banners',
        name: 'BannerManagement',
        component: BannerManagement,
        meta: { title: '轮播图管理' }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: Settings,
        meta: { title: '系统设置' }
      },
      {
        path: 'profile',
        name: 'AdminProfile',
        component: AdminProfile,
        meta: { title: '管理员信息' }
      }
    ]
  },
  
  // 重定向到首页
  {
    path: '*',
    redirect: '/'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 需要认证的路由检查
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查是否登录
    if (!store.getters.isLoggedIn) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      // 检查角色权限
      if (to.matched.some(record => record.meta.requiresRole)) {
        const requiredRole = to.matched.find(record => record.meta.requiresRole).meta.requiresRole
        if (store.getters.userRole !== requiredRole) {
          next({ path: '/' })
        } else {
          next()
        }
      } else {
        next()
      }
    }
  } else {
    next()
  }
})

export default router 